/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlparser;

/**
 *
 * @author pbelov
 */
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import base.*;
import java.util.Vector;

public class userParser extends DefaultHandler{

    User tuser;
    Vector vu = new Vector();

    String thisElement = "";

    public static String USER = "user";
    public static String NAME = "name";
    public static String LOGIN = "login";
    public static String PRIV = "priv";
    public static String MAIL = "mail";
    public static String TEAM = "team";
    public static String TEAMLEADER = "teamleader";

    public userParser() {
        vu.setSize(0);
    }

    public void fill(User[] users) {
        for (int i = 0; i < users.length; i++) {
            users[i] = (User)vu.elementAt(i);
        }
    }

    public User getUser(int i) {
        return (User)vu.elementAt(i);
    }

    public int getCount() {
        return vu.size();
    }

    @Override
    public void startDocument() throws SAXException {
      System.out.println("Start parse users XML...");
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        thisElement = qName;

        if (thisElement.equals(USER)) {
            tuser = new User();
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (qName.equals(USER)) {
            vu.add(tuser);
        }

        thisElement = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

      if (thisElement.equals(PRIV)) {
         tuser.setPriv(new Integer(new String(ch, start, length)));
      }

      if (thisElement.equals(NAME)) {
         tuser.setName(new String(ch, start, length));
      }

      if (thisElement.equals(LOGIN)) {
         tuser.setLogin(new String(ch, start, length));
      }

      if (thisElement.equals(MAIL)) {
         tuser.setMail(new String(ch, start, length));
      }

      if (thisElement.equals(TEAM)) {
         tuser.setTeamName(new String(ch, start, length));
      }

      if (thisElement.equals(TEAMLEADER)) {
          tuser.setTeamLeader(new String(ch, start, length));
      }
    }

    @Override
    public void endDocument() {
      System.out.println("Stop parse users XML...");
    }
}